Scanner assignment

Description

Write the scanner, the first phase of your compiler.  The scanner reads in all tokens of an input program and generates an output program with some tokens modified.  Both input and output program are correct C programs.  The specific requirements for code generation are: For example, the following program should be converted as follows.

example program :  foo.c

    #include <stdio.h>
    #define read(x) scanf("%d\n", &x)
    #define write(x) printf("%d\n", x)

    void foo() {
        int a;
        read(a);
        write(a);
    }

    int main() {
        foo();
    }

running your scanner:

% scanner foo.c

generated program (no formatting needed): foo_cs254.c

    #include <stdio.h>
    #define read(x) scanf("%d\n", &x)
    #define write(x) printf("%d\n", x)

   void cs254foo() {
        int cs254a;
        read(cs254a);
        write(cs254a);
    }

   int main() {
        cs254foo();
    }
 

The name change should not change the result of the program.  You can test the correctness of your scanner by comparing the execution result of a program before and after the name change.
 

Recommended interface of the scanner object:

With this interface, you can scan through and print a program as follows:

    Scanner scan1("test1.c");  // Copy meta-statements.  Initialize the scanner.
    While (scan1.HasMoreTokens()) {
        print(scan1.CurrentToken());  // If it is an ID, add cs254 to its name (except for "main").
        scan1.MoveToNextToken();
    }

Test programs

A list of 8 standard test programs can be downloaded from here.  Scroll down to see the content of README.